package pers.xukai.fullMark.service.impl;/**
 * @author 殷绪凯
 * @since 2022/11/29
 */

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.ObjectUtil;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import pers.xukai.common.constant.GlobalConstant;
import pers.xukai.common.entity.po.User;
import pers.xukai.common.exception.BizException;
import pers.xukai.common.repository.UserRepository;
import pers.xukai.fullMark.dao.TodoDao;
import pers.xukai.fullMark.entity.bo.EmailTaskBo;
import pers.xukai.fullMark.entity.po.Todo;
import pers.xukai.fullMark.entity.vo.SaveTodoVo;
import pers.xukai.fullMark.repository.TodoRepository;
import pers.xukai.fullMark.service.TodoService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @author 殷绪凯
 * @datetime 2022年 11月 29日 21:14
 */
@Service
@Transactional
public class TodoServiceImpl implements TodoService {

    private final TodoRepository todoRepository;
    private final TodoDao todoDao;
    private final UserRepository userDao;
    private final RedisTemplate<String, EmailTaskBo> redisTemplate;

    public TodoServiceImpl(TodoRepository todoRepository, TodoDao todoDao, UserRepository userDao, RedisTemplate<String, EmailTaskBo> redisTemplate) {
        this.todoRepository = todoRepository;
        this.todoDao = todoDao;
        this.userDao = userDao;
        this.redisTemplate = redisTemplate;
    }

    @Override
    public void save(SaveTodoVo todoVo) {
        todoVo.check();
        Date todoStartTime = todoVo.getTodoStartTime();
        Date todoEndTime = todoVo.getTodoEndTime();
        //对时间进行校验
        if (ObjectUtil.isAllNotEmpty(todoStartTime, todoEndTime) && todoStartTime.after(todoEndTime)) {
            throw new BizException("开始时间不能晚于结束时间！");
        }
        //保存待办
        Todo todo = todoVo.createTodo();
        //校验用户是否存在
        String userId = todo.getUserId();
        User user = userDao.findById(userId).orElseThrow(() -> new BizException("未查询到当前用户信息！"));
        todo = todoRepository.save(todo);
        //是否保存成功
        boolean saveSuccess = !ObjectUtil.isNull(todo) && !ObjectUtil.isNull(todo.getTodoId());
        if (saveSuccess && todo.isSendMsg()) {
            Date noticeTime = todo.getNoticeTime();
            //将邮件内容放到redis中，以便定时任务读取
            String redisKey = GlobalConstant.REDIS_KEY_EMAIL_TASK + DateUtil.format(noticeTime, "yyyyMMdd");
            String hashKey = todo.getTodoId();
            redisTemplate.opsForHash()
                    .put(
                            redisKey,
                            hashKey,
                            new EmailTaskBo(user.getEmail(), user.getUsername(), noticeTime, todo.getContent())
                    );
        }
    }

    @Override
    public void delete(String id) {
        todoRepository.deleteById(id);
    }

    @Override
    public void test() {
        Map one = todoDao.getOne();
        System.out.println("mybatis:\n"+one);
        List<Todo> all = todoRepository.findAll();
        System.out.println("jpa:\n"+all);
    }
}
